package com.example.maze;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DatabaseHandler extends SQLiteOpenHelper {
	
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "mazeDB";
    
    //table to store the player's name and the link to the table with their results
    public static abstract class LeaderBoardTable implements BaseColumns {
        public static final String TABLE_NAME = "leaderBoard";
        public static final String COLUMN_NAME_NAME = "name";
        public static final String COLUMN_NAME_SCORE_ID = "scoreid";
    }
    
    //table to store all the information from a player's game
    public static abstract class ScoreTable implements BaseColumns {
        public static final String TABLE_NAME = "score";
        public static final String COLUMN_NAME_SCORE = "score";
        public static final String COLUMN_NAME_MOVES = "moves";
        public static final String COLUMN_NAME_TIME = "time";
        public static final String COLUMN_NAME_SIZE = "size";
        public static final String COLUMN_NAME_PERCENT = "percent";
        public static final String COLUMN_NAME_TYPE = "type";
    }
    
    //setting the column names
    private static final String SQL_CREATE_LEADER_BOARD_TABLE = "CREATE TABLE " + LeaderBoardTable.TABLE_NAME + " (" 
    													+ LeaderBoardTable._ID + " INTEGER PRIMARY KEY," 
    													+ LeaderBoardTable.COLUMN_NAME_NAME +" TEXT,"
    													+ LeaderBoardTable.COLUMN_NAME_SCORE_ID  + " INTEGER)";
    
    //setting the column names
    private static final String SQL_CREATE_SCORE_TABLE = "CREATE TABLE " + ScoreTable.TABLE_NAME + " (" 
                                                        + ScoreTable._ID + " INTEGER PRIMARY KEY," 
                                                        + ScoreTable.COLUMN_NAME_SCORE + " REAL,"
                                                        + ScoreTable.COLUMN_NAME_MOVES + " INTEGER,"
                                                        + ScoreTable.COLUMN_NAME_TIME + " INTEGER,"
                                                        + ScoreTable.COLUMN_NAME_SIZE + " INTEGER,"
                                                        + ScoreTable.COLUMN_NAME_PERCENT + " INTEGER,"
                                                        + ScoreTable.COLUMN_NAME_TYPE + " INTEGER )";
    
    //to make it possible to upgrade table have to delete old ones and make new ones
    private static final String SQL_DELETE_LEADER_BOARD_TABLE  = "DROP TABLE IF EXISTS " 
    													+ LeaderBoardTable.TABLE_NAME;
        
    private static final String SQL_DELETE_SCORE_TABLE = "DROP TABLE IF EXISTS " 
    													+ ScoreTable.TABLE_NAME;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_LEADER_BOARD_TABLE);
        db.execSQL(SQL_CREATE_SCORE_TABLE);
    }
    
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL(SQL_DELETE_LEADER_BOARD_TABLE);
        db.execSQL(SQL_DELETE_SCORE_TABLE);
        // Create tables again
        onCreate(db);
    }
    
}
